<template>
  <div id="mountNode" class="flow-chart"></div>
</template>

<script setup>
import { onMounted } from 'vue';
import G6 from '@antv/g6/dist/g6.min';
import { useRoute, useRouter } from 'vue-router';
import dagre from 'dagre/dist/dagre';
import { ElMessage } from 'element-plus';
import { httpDeviationComputingResult } from '@/api/antiJamPerformCalculateSystem';

let $router = useRouter();
const route = useRoute();
onMounted(() => {
  initComputeAtlas();
});

const props = defineProps({
  sampleId: {
    type: String,
    required: true,
  },
  hisSampleId: {
    type: String,
    required: true,
  },
  deviationFuseResult: {
    type: String,
    required: true,
  },
});

const initComputeAtlas = () => {
  const data = {
    nodes: [
      {
        id: '历史样本一',
      },
      {
        id: '导引头参数不同的性能偏差估计',
      },
      {
        id: '压制类干扰参数不同的性能偏差估计',
      },
      {
        id: '欺骗类干扰参数不同的性能偏差估计',
      },
      { 
        id: '组合类干扰参数不同的性能偏差估计' 
      },
      {
        id: `融合计算结果:${ route.query.deviationFuseResult }`,
        deviationFuseResult: route.query.deviationFuseResult || ''
      },
      {
        id: '抗干扰性能推算结果',
      }
    ],
    edges: [
      {
        source: '历史样本一',
        target: '导引头参数不同的性能偏差估计',
      },
      {
        source: '历史样本一',
        target: '压制类干扰参数不同的性能偏差估计',
      },
      {
        source: '历史样本一',
        target: '欺骗类干扰参数不同的性能偏差估计',
      },
      {
        source: '历史样本一',
        target: '组合类干扰参数不同的性能偏差估计',
      },
      {
        source: '导引头参数不同的性能偏差估计',
        target: `融合计算结果:${ route.query.deviationFuseResult }`,
      },
      {
        source: '压制类干扰参数不同的性能偏差估计',
        target: `融合计算结果:${ route.query.deviationFuseResult }`,
      },
      {
        source: '欺骗类干扰参数不同的性能偏差估计',
        target: `融合计算结果:${ route.query.deviationFuseResult }`,
      },
      {
        source: '组合类干扰参数不同的性能偏差估计',
        target: `融合计算结果:${ route.query.deviationFuseResult }`,
      },
      {
        source: `融合计算结果:${ route.query.deviationFuseResult}`,
        target: '抗干扰性能推算结果',
      },
    ],
  };
  var g = new dagre.graphlib.Graph();
  g.setDefaultEdgeLabel(function () {
    return {};
  });
  g.setGraph({
    rankdir: 'TB',
  });
  data.nodes.forEach(function (node) {
    node.label = node.id;
    g.setNode(node.id, {
      width: 280,
      height: 150,
    });
  });
  data.edges.forEach(function (edge) {
    g.setEdge(edge.source, edge.target);
  });
  dagre.layout(g);
  var coord = void 0;
  g.nodes().forEach(function (node, i) {
    coord = g.node(node);
    data.nodes[i].x = coord.x;
    data.nodes[i].y = coord.y;
  });
  g.edges().forEach(function (edge, i) {
    coord = g.edge(edge);
    data.edges[i].startPoint = coord.points[0];
    data.edges[i].endPoint = coord.points[coord.points.length - 1];
    data.edges[i].controlPoints = coord.points.slice(
      1,
      coord.points.length - 1
    );
  });
  G6.registerNode(
    'operation',
    {
      drawShape: function drawShape(cfg, group) {
        var rect = group.addShape('rect', {
          attrs: {
            x: -150,
            y: -35,
            width: 300,
            height: 70,
            radius: 5,
            fill: 'rgb(1, 18, 90)',
            lineWidth: 0,
          },
        });
        return rect;
      },
    },
    'single-shape'
  );

  var graph = new G6.Graph({
    container: 'mountNode',
    width: 1000,
    height: 500,
    pixelRatio: 2,
    modes: {
      default: ['drag-canvas', 'zoom-canvas'],
    },
    defaultNode: {
      shape: 'operation',
      type: 'ellipse',
      labelCfg: {
        style: {
          fill: 'rgb(255, 255, 255)',
          fontSize: 17,
          fontWeight: 'bold',
          cursor: 'pointer',
        },
      },
    },
    defaultEdge: {
      shape: 'polyline',
      style: {
        endArrow: true,
        lineWidth: 2,
        stroke: 'rgb(1, 18, 90)',
      },
    },
  });
  graph.data(data);
  graph.render();
  graph.fitView();
  // 节点点击事件
  graph.on('node:click', (ev) => {
    const nodeId = ev.item._cfg.id;
    if(nodeId == "导引头参数不同的性能偏差估计"){
      const params = {
        sampleId: props.sampleId,
        hisSampleId: props.hisSampleId,
        type: "1",
      };

      httpDeviationComputingResult(params).then((res) => {
        if (res.code == 200) {
          $router.push({
          path: '/antiJamPerformCalculateSystem/deviationCalculateResult',
          query: {
            nodeId: '导引头参数不同的性能偏差',
            sampleId: props.sampleId,
            hisSampleId: props.hisSampleId,
            deviationFuseResult: route.query.deviationFuseResult
          },
        });
        } else {
          ElMessage({
            message: res.msg,
            type: 'error',
          });
        }
      });      
    }

    if(nodeId == "压制类干扰参数不同的性能偏差估计"){
      const params = {
        sampleId: props.sampleId,
        hisSampleId: props.hisSampleId,
        type: "3",
      };

      httpDeviationComputingResult(params).then((res) => {
        if (result.code == 200) {
          $router.push({
          path: '/antiJamPerformCalculateSystem/deviationCalculateResult',
          query: {
            nodeId: '压制类干扰参数不同的性能偏差估计',
            sampleId: props.sampleId,
            hisSampleId: props.hisSampleId,
             deviationFuseResult: route.query.deviationFuseResult
          },
        });
        } else {
          ElMessage({
            message: res.msg,
            type: 'error',
          });
        }
      });      
    }

    if(nodeId == "欺骗类干扰参数不同的性能偏差估计"){
      const params = {
        sampleId: props.sampleId,
        hisSampleId: props.hisSampleId,
        type: "2",
      };

      httpDeviationComputingResult(params).then((res) => {
        if (result.code == 200) {
          $router.push({
          path: '/antiJamPerformCalculateSystem/deviationCalculateResult',
          query: {
            nodeId: '欺骗类干扰参数不同的性能偏差估计',
            sampleId: props.sampleId,
            hisSampleId: props.hisSampleId,
            deviationFuseResult: route.query.deviationFuseResult
          },
        });
        } else {
          ElMessage({
            message: res.msg,
            type: 'error',
          });
        }
      });      
    }

    if(nodeId == "组合类干扰参数不同的性能偏差估计"){
      const params = {
        sampleId: props.sampleId,
        hisSampleId: props.hisSampleId,
        type: "4",
      };

      httpDeviationComputingResult(params).then((res) => {
        if (result.code == 200) {
          $router.push({
          path: '/antiJamPerformCalculateSystem/deviationCalculateResult',
          query: {
            nodeId: '组合类干扰参数不同的性能偏差估计',
            sampleId: props.sampleId,
            hisSampleId: props.hisSampleId,
            deviationFuseResult: route.query.deviationFuseResult
          },
        });
        } else {
          ElMessage({
            message: res.msg,
            type: 'error',
          });
        }
      });      
    }
  });

};
</script>

<style lang="scss" scoped>
.flow-chart {
}
</style>
